package org.kyw.pet.controller;

import org.kyw.basic.exception.BusinessException;
import org.kyw.basic.util.AjaxResult;
import org.kyw.basic.util.LoginContext;
import org.kyw.basic.util.PageList;
import org.kyw.pet.domain.Pet;
import org.kyw.pet.domain.SearchMasterMsg;
import org.kyw.pet.query.SearchMasterMsgQuery;
import org.kyw.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            seachMasterMsgService.publish(searchMasterMsg, LoginContext.getCurrentLogin(request));
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("报错了，正在殴打程序员");
        }
    }

    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet, LoginContext.getCurrentLogin(request));
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("报错了，正在殴打程序员");
        }
    }

    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.queryAll();
    }

    @PostMapping
    public PageList<SearchMasterMsg> list(@RequestBody SearchMasterMsgQuery msgQuery, HttpServletRequest request){
        return seachMasterMsgService.list(msgQuery, LoginContext.getCurrentLogin(request));
    }
}
